package cn.wolfcode.trip.admin.web.controller;

import cn.wolfcode.trip.admin.util.UploadUtil;
import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.query.DailyQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IDailyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/daily")
public class DailyController {
    @Autowired
    private IDailyService dailyService;
    @Autowired
    private IRegionService regionService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") DailyQueryObject qo, Model model) {
        model.addAttribute("pageInfo", dailyService.query(qo));
        model.addAttribute("places", regionService.list());
        return "daily/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Daily entity, MultipartFile img) {
        if (img != null && img.getContentType().startsWith("image") && img.getSize() > 0) {
            String url = UploadUtil.uploadQiniu(img);
            entity.setCoverUrl(url);
        }
        dailyService.saveOrUpdate(entity);
        return "redirect:/daily/list.do";
    }
}
